package com.book.service.impl;

import com.book.dao.BrowseHistoryDao;
import com.book.dto.BrowseHistoryDto;
import com.book.helper.BrowseHistoryHelper;
import com.book.pojo.BrowseHistory;
import com.book.service.BrowseHistoryService;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @version 1.0
 * @Author Jincheng.Li
 * @Date 2021/5/29 9:10 下午
 * @Project_Name graduation-project
 */
@Service
public class BrowseHistoryServiceImpl implements BrowseHistoryService {
    @Autowired
    private BrowseHistoryDao browseHistoryDao;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return browseHistoryDao.deleteByPrimaryKey(id);
    }

    @Override
    public int delete(String uCode) {
        return browseHistoryDao.delete(uCode);
    }

    @Override
    public List<BrowseHistoryDto> getList(String uCode) {
        List<BrowseHistory> list = browseHistoryDao.getList(uCode);
        if (CollectionUtils.isEmpty(list)) {
            return Lists.newArrayList();
        }
        return list.stream().map(BrowseHistoryHelper::covertPoToDto).collect(Collectors.toList());
    }

    @Override
    public int insertSelective(BrowseHistory record) {
        return browseHistoryDao.insertSelective(record);
    }

    @Override
    public int updateByPrimaryKeySelective(BrowseHistory record) {
        return browseHistoryDao.updateByPrimaryKeySelective(record);
    }

    @Override
    public BrowseHistoryDto getBrowseHistory(String uCode, String bookCode) {
        BrowseHistory browseHistory = browseHistoryDao.getBrowseHistory(uCode, bookCode);
        return browseHistory == null ? null : BrowseHistoryHelper.covertPoToDto(browseHistory);
    }
}
